<?php
namespace App\Http\Controllers\Passenger\V2;

use App\Http\Controllers\Controller;
use App\Models\PayOrder;
use App\Models\RechargeRecord;
use App\Models\RefundOrder;
use App\Services\PassengerLoginService;
use Illuminate\Http\Request;
use Overtrue\LaravelWeChat\Facade;

/**
 * 用户登录相关
 * Class LoginController
 * @package App\Http\Controllers\Passenger\User
 */
class LoginController extends Controller
{


    /**
     * 微信小程序登录
     */
    public  function wxminLogin(Request $request, PassengerLoginService $loginService)
    {
        $code          = (string) $request->post('code');
        $iv            = (string) $request->post('iv');
        $encryptedData = (string) $request->post('encryptedData');

        $login_info = $loginService->wxminLogin($code, $iv, $encryptedData);
        return $this->sendJson($login_info);
    }


    /**
     * 账号登录
     */
    public function accountLogin(Request $request, PassengerLoginService $loginService)
    {
        $username   = (string) $request->post('username', '');
        $password   = (string) $request->post('password', '');

        if( !($username && $password) )
            return $this->sendError('用户名或密码不能为空');

        $login_info = $loginService->accountLogin($username, $password);
        return $this->sendJson($login_info);
    }


    /**
     * 手机账号登录
     */
    public function phoneLogin(Request $request, PassengerLoginService $loginService)
    {

        $phone = (string) $request->post('phone', '');
        $code  = (string) $request->post('code', '');

        if( !($phone && $code) )
            return $this->sendError('手机号或验证码不能为空');

        $login_info = $loginService->phoneLogin($phone, $code);
        return $this->sendJson($login_info);
    }


    /**
     * 测试退款接口 todo 上线删除
     */
    public function testRefund(Request $request)
    {
        $sn = $request->post('sn');
        $pay_order_info = PayOrder::query()->where('sn', $sn)->first();
        $refunded_price = RefundOrder::query()->where([
            'status'=> 2,
            'pay_order_sn' => $sn,
        ])->sum('price');
        $refund_price = $pay_order_info->price - $refunded_price;
        if(!$pay_order_info)
            exit('SN有误');

        $app = Facade::payment();
        $re  = $app->refund->byOutTradeNumber(
            $pay_order_info->sn, //支付订单号
            uniqid(), //退款号
            intval(ceil($pay_order_info->price * 100)), //支付金额
            intval(ceil($refund_price * 100 ))//退款金额阿
        );

        if($re['result_code'] !== 'SUCCESS') //申请退款失败
            exit($re['err_code_des']);

        exit('success');
    }

    /**
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * 充值退款金额
     */
    public function cheshi(Request $request)
    {
        $sn   = $request->get('sn');
        $info = RechargeRecord::query()->where('order_sn',$sn)->first();
        if(!$info)return_bob('订单号不存在');
        $app = Facade::payment();
        $re  = $app->refund->byOutTradeNumber(
            $info->order_sn, //支付订单号
            uniqid(), //退款号
            intval(ceil($info->price * 100)), //支付金额
            intval(ceil($info->price * 100 ))//退款金额阿
        );
        if($re['result_code'] !== 'SUCCESS') //申请退款失败
            exit($re['err_code_des']);

        exit('success');
    }
}
